package controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;

import beans.LogEnum;
import dao.AlgorithmDao;
import dao.ModelDao;
import net.sf.json.JSONObject;
import portal.controller.BaseServlet;
import utils.UserLogUtil;

/**
 * Servlet implementation class AddModel
 */
public class AddModel extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static ModelDao modelDao = new ModelDao();
	@Autowired
	private static AlgorithmDao algorithmDao ;//= new AlgorithmDao();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddModel() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		JSONObject dataJson = JSONObject.fromObject(request.getParameter("data"));
		String operateType = request.getParameter("operateType");

		switch (operateType) {
		case "addModel":
			doAddModel(request, response, dataJson);
			break;
		}

	}

	/**
	 * 添加模型方法 By尹行
	 * 
	 * @param request
	 * @param response
	 * @param dataJson
	 * @throws IOException
	 * @throws ParseException
	 */
	private void doAddModel(HttpServletRequest request, HttpServletResponse response, JSONObject dataJson) throws IOException {
		// 获取userId
		String userId = (String) request.getSession().getAttribute("userId");
		// 获取json结果
		String name = dataJson.getString("name");
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime = df.format(new Date());
		Integer nesting = Integer.parseInt(dataJson.getString("isNesting"));
		String nestJson = dataJson.getString("nestJson");
		String json = dataJson.getString("json");
		Integer reuse = Integer.parseInt(dataJson.getString("isReuse"));
		String introduction = dataJson.getString("introduction");
		Integer modelId = -1;
		// 返回结果
		Boolean res = false;
		List<Object> results = modelDao.addModelReturnId(name, userId, createTime, nesting, nestJson, json,
				reuse, introduction,0);
		if (results.size() > 0) {
			res = true;
			modelId = (int) results.get(0);
		}
		JSONObject result = new JSONObject();
		result.put("modelId", modelId);
		result.put("result", res);
		// 返回json
		response.getWriter().print(result.toString());
		// 日志记录
		UserLogUtil.dbLog(LogEnum.model, LogEnum.add,
				userId, LogEnum.model.getValue()+"编号："+modelId+"，名称："+name);
	}

}
